﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;

namespace TanmiaGrp.Basis
{
    public static class Extensions
    {
        /// <summary>
        /// Appends the specified string in a new line or the first line when the <see cref="StringBuilder"/> was empty.
        /// </summary>
        /// <param name="stringBuilder">The string builder.</param>
        /// <param name="value">The string to append.</param>
        /// <returns>A reference to this instance after the append operation has completed.</returns>
        /// <exception cref="ArgumentOutOfRangeException">Enlarging the value of this instance would exceed System.Text.StringBuilder.MaxCapacity.</exception>
        public static StringBuilder AppendInNewLine(this StringBuilder stringBuilder, string value)
        {
            if (stringBuilder == null)
                throw new ArgumentNullException("stringBuilder");

            if (stringBuilder.Length == 0)
                stringBuilder.Append(value);
            else
                stringBuilder.Append(Environment.NewLine + value);

            return stringBuilder;
        }

        public static string ToPropertyName<T>(this Expression<Func<T, object>> selector)
        {
            var me = selector.Body as MemberExpression;
            if (me == null)
            {
                throw new ArgumentException("MemberException expected.");
            }

            var propertyName = me.ToString().Remove(0, 2);
            return propertyName;
        }
    }
}
